import java.awt.Point;


public class MoveTo implements State 
{
	private FSMachine	m_fsm;
	private Point		m_desPos;
	private Point		m_curPos;
	
	private int myID;
	private int otherID;
	
	public MoveTo(FSMachine fsm)
	{
		m_fsm = fsm;
	}
	
	public void Enter(int myID, int otherID) {
		Global.agents.get(myID).isBusy = true;
		Global.agents.get(otherID).isBusy = true;
		this.myID = myID;
		this.otherID = otherID;
	}
	
	public void Execute() {
		System.out.println("Agent " + myID + " is moving towards Agent "
				+ otherID);
		double deltaX = Global.agents.get(myID).locx
				- Global.agents.get(otherID).locx;
		double deltaY = Global.agents.get(myID).locy
				- Global.agents.get(otherID).locy;
		
		double distance = Global.agents.get(myID).distanceTo(otherID);
		
		if (distance > Global.WALKING_SPEED) {
			deltaX = Global.agents.get(myID).locx
					- Global.agents.get(otherID).locx;
			deltaY = Global.agents.get(myID).locy
					- Global.agents.get(otherID).locy;
			
			double theta = Math.atan2(deltaY, deltaX);
			
			// System.out.println("DeltaX="+deltaX+" DeltaY="+deltaY+" Agente "+myID);
			// System.out.println("X="+Global.agents.get(myID).locx);
			// System.out.println("Y="+Global.agents.get(myID).locy);
				Global.agents.get(myID).locx = Global.agents.get(myID).locx
						- Global.WALKING_SPEED*Math.cos(theta);
				Global.agents.get(myID).locy = Global.agents.get(myID).locy
						- Global.WALKING_SPEED*Math.sin(theta);
		} else {
			System.out.println("Agent " + myID + " is next to Agent " + otherID);
			
			if (!Global.agents.get(myID).isKiller()) {
                            try{
				m_fsm.ChangeState(FSMachine.enumState.TALKTO, myID, otherID);}
                            catch(StackOverflowError se){
                                System.out.println("ERROR CAUGHT");
                            }
			} else {
                try{
                    // If I'm at my friend, talk to him. Otherwise. Kill him.
                    if (Global.agents.get(myID).enemies.contains(otherID) && !Global.killInProgress) {
                        m_fsm.ChangeState(FSMachine.enumState.KILLAGENT, myID, otherID);
                    } else {
                        m_fsm.ChangeState(FSMachine.enumState.TALKTO, myID, otherID);
                    }
                } catch(StackOverflowError se){
                    System.out.println("ERROR CAUGHT");
                }
			}
		}
		// Pseudo-code
		// call an API for moving an agent to a given destination.
		// if(m_desPos != m_curPos)
		// {
		// moveTo();
		// m_curPos = GetPos();
		// }
		// else
		// {
		// m_fsm.ChangeState(FSMachine.enumState.GUESS);
		// }
	}
	
	public void Exit()
	{
                
		//System.out.println("MoveTo - Exit");           
		Global.agents.get(myID).isBusy = false;
		Global.agents.get(otherID).isBusy = false;	
	}
	
	public void SetDesPos(Point desPos)
	{
		m_desPos = desPos;
	}
	
}
